home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / qe4 / eclass.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-10  |  4.6 KB  |  261 lines

  1.  
  2. #include "qe3.h"
  3. #include "io.h"
  4.  
  5. eclass_t    *eclass;
  6. eclass_t    *eclass_bad;
  7. char        eclass_directory[1024];
  8.  
  9. /*
  10.  
  11. the classname, color triple, and bounding box are parsed out of comments
  12. A ? size means take the exact brush size.
  13.  
  14. /*QUAKED <classname> (0 0 0) ?
  15. /*QUAKED <classname> (0 0 0) (-8 -8 -8) (8 8 8)
  16.  
  17. Flag names can follow the size description:
  18.  
  19. /*QUAKED func_door (0 .5 .8) ? START_OPEN STONE_SOUND DOOR_DONT_LINK GOLD_KEY SILVER_KEY
  20.  
  21. */
  22. char    *debugname;
  23.  
  24. eclass_t *Eclass_InitFromText (char *text)
  25. {
  26.     char    *t;
  27.     int        len;
  28.     int        r, i;
  29.     char    parms[256], *p;
  30.     eclass_t    *e;
  31.     char    color[128];
  32.  
  33.     e = qmalloc(sizeof(*e));
  34.     memset (e, 0, sizeof(*e));
  35.     
  36.     text += strlen("/*QUAKED ");
  37.     
  38. // grab the name
  39.     text = COM_Parse (text);
  40.     e->name = qmalloc (strlen(com_token)+1);
  41.     strcpy (e->name, com_token);
  42.     debugname = e->name;
  43.     
  44. // grab the color, reformat as texture name
  45.     r = sscanf (text," (%f %f %f)", &e->color[0], &e->color[1], &e->color[2]);
  46.     if (r != 3)
  47.         return e;
  48.     sprintf (color, "(%f %f %f)", e->color[0], e->color[1], e->color[2]);
  49.     strcpy (e->texdef.name, color);
  50.  
  51.     while (*text != ')')
  52.     {
  53.         if (!*text)
  54.             return e;
  55.         text++;
  56.     }
  57.     text++;
  58.     
  59. // get the size    
  60.     text = COM_Parse (text);
  61.     if (com_token[0] == '(')
  62.     {    // parse the size as two vectors
  63.         e->fixedsize = true;
  64.         r = sscanf (text,"%f %f %f) (%f %f %f)", &e->mins[0], &e->mins[1], &e->mins[2],
  65.             &e->maxs[0], &e->maxs[1], &e->maxs[2]);
  66.         if (r != 6)
  67.             return e;
  68.  
  69.         for (i=0 ; i<2 ; i++)
  70.         {
  71.             while (*text != ')')
  72.             {
  73.                 if (!*text)
  74.                     return e;
  75.                 text++;
  76.             }
  77.             text++;
  78.         }
  79.     }
  80.     else
  81.     {    // use the brushes
  82.     }
  83.     
  84. // get the flags
  85.     
  86.  
  87. // copy to the first /n
  88.     p = parms;
  89.     while (*text && *text != '\n')
  90.         *p++ = *text++;
  91.     *p = 0;
  92.     text++;
  93.     
  94. // any remaining words are parm flags
  95.     p = parms;
  96.     for (i=0 ; i<8 ; i++)
  97.     {
  98.         p = COM_Parse (p);
  99.         if (!p)
  100.             break;
  101.         strcpy (e->flagnames[i], com_token);
  102.     } 
  103.  
  104. // find the length until close comment
  105.     for (t=text ; t[0] && !(t[0]=='*' && t[1]=='/') ; t++)
  106.     ;
  107.     
  108. // copy the comment block out
  109.     len = t-text;
  110.     e->comments = qmalloc (len+1);
  111.     memcpy (e->comments, text, len);
  112. #if 0
  113.     for (i=0 ; i<len ; i++)
  114.         if (text[i] == '\n')
  115.             e->comments[i] = '\r';
  116.         else
  117.             e->comments[i] = text[i];
  118. #endif
  119.     e->comments[len] = 0;
  120.     
  121.     return e;
  122. }
  123.  
  124.  
  125. /*
  126. =================
  127. Eclass_InsertAlphabetized
  128. =================
  129. */
  130. void Eclass_InsertAlphabetized (eclass_t *e)
  131. {
  132.     eclass_t    *s;
  133.     
  134.     if (!eclass)
  135.     {
  136.         eclass = e;
  137.         return;
  138.     }
  139.  
  140.  
  141.     s = eclass;
  142.     if (stricmp (e->name, s->name) < 0)
  143.     {
  144.         e->next = s;
  145.         eclass = e;
  146.         return;
  147.     }
  148.  
  149.     do
  150.     {
  151.         if (!s->next || stricmp (e->name, s->next->name) < 0)
  152.         {
  153.             e->next = s->next;
  154.             s->next = e;
  155.             return;
  156.         }
  157.         s=s->next;
  158.     } while (1);
  159. }
  160.  
  161.  
  162. /*
  163. =================
  164. Eclass_ScanFile
  165. =================
  166. */
  167. void Eclass_ScanFile (char *filename)
  168. {
  169.     int        size;
  170.     char    *data;
  171.     eclass_t    *e;
  172.     int        i;
  173.     char    temp[1024];
  174.  
  175.     QE_ConvertDOSToUnixName( temp, filename );
  176.  
  177.     Sys_Printf ("ScanFile: %s\n", temp);
  178.  
  179.     size = LoadFile (filename, (void *)&data);
  180.     
  181.     for (i=0 ; i<size ; i++)
  182.         if (!strncmp(data+i, "/*QUAKED",8))
  183.         {
  184.             e = Eclass_InitFromText (data+i);
  185.             if (e)
  186.                 Eclass_InsertAlphabetized (e);
  187.             else
  188.                 printf ("Error parsing: %s in %s\n",debugname, filename);
  189.         }
  190.         
  191.     free (data);
  192. }
  193.  
  194.  
  195.  
  196. void Eclass_InitForSourceDirectory (char *path)
  197. {
  198.     struct _finddata_t fileinfo;
  199.     int        handle;
  200.     char    filename[1024];
  201.     char    filebase[1024];
  202.     char    temp[1024];
  203.     char    *s;
  204.  
  205.     QE_ConvertDOSToUnixName( temp, path );
  206.  
  207.     Sys_Printf ("Eclass_InitForSourceDirectory: %s\n", temp );
  208.  
  209.     strcpy (filebase, path);
  210.     s = filebase + strlen(filebase)-1;
  211.     while (*s != '\\' && *s != '/' && s!=filebase)
  212.         s--;
  213.     *s = 0;
  214.  
  215.     eclass = NULL;
  216.  
  217.     handle = _findfirst (path, &fileinfo);
  218.     if (handle != -1)
  219.     {
  220.         do
  221.         {
  222.             sprintf (filename, "%s\\%s", filebase, fileinfo.name);
  223.             Eclass_ScanFile (filename);
  224.         } while (_findnext( handle, &fileinfo ) != -1);
  225.  
  226.         _findclose (handle);
  227.     }
  228.  
  229.     eclass_bad = Eclass_InitFromText ("/*QUAKED UNKNOWN_CLASS (0 0.5 0) ?");
  230. }
  231.  
  232. eclass_t *Eclass_ForName (char *name, qboolean has_brushes)
  233. {
  234.     eclass_t    *e;
  235.     char        init[1024];
  236.  
  237.     if (!name)
  238.         return eclass_bad;
  239.  
  240.     for (e=eclass ; e ; e=e->next)
  241.         if (!strcmp (name, e->name))
  242.             return e;
  243.  
  244.     // create a new class for it
  245.     if (has_brushes)
  246.     {
  247.         sprintf (init, "/*QUAKED %s (0 0.5 0) ?\nNot found in source.\n", name);
  248.         e = Eclass_InitFromText (init);
  249.     }
  250.     else
  251.     {
  252.         sprintf (init, "/*QUAKED %s (0 0.5 0) (-8 -8 -8) (8 8 8)\nNot found in source.\n", name);
  253.         e = Eclass_InitFromText (init);
  254.     }
  255.  
  256.     Eclass_InsertAlphabetized (e);
  257.  
  258.     return e;
  259. }
  260.  
  261.